package com.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @author:王宇航
 * @create: 2022-12-01 22:24
 * @Description:
 */
@Component
public class uploadImage {
    /*阿里域名*/
    public static final String ALI_url = "https://wyh-777.oss-cn-nanjing.aliyuncs.com/";
    public static String upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();//原来的图片名
        String ext = "." + FilenameUtils.getExtension(originalFilename);
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String fileName = uuid + ext;
        /*地域节点*/
        String endpoint="https://oss-cn-nanjing.aliyuncs.com";
        String accessKeyId="LTAI5t997NaxufBZZyW37xoY";
        String accessKeySecret="rEM68k64Xpi6G7jcLpWWQgNwUIDfxM";
        /*OSS客户端对象*/
        OSS oss=new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
        oss.putObject(
                "wyh-777", //仓库名
                fileName,
                file.getInputStream()
        );
        oss.shutdown();
        return ALI_url+fileName;
    }
}
